package com.ylbh.modules.shoptemplate.controller;

import java.util.*;

import com.ylbh.common.annotation.SysLog;
import com.ylbh.common.pojo.shoptemplate.dto.CommodityBankDTO;
import com.ylbh.common.pojo.shoptemplate.vo.ShopClassVO;
import com.ylbh.common.pojo.upload.vo.UploadItem;
import com.ylbh.common.utils.*;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import com.ylbh.modules.shoptemplate.entity.ShoppingShopTemplate;
import com.ylbh.modules.shoptemplate.service.ShoppingShopTemplateService;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;


/**
 * 店铺模版
 *
 * @author czx
 * @date 2019-12-20 15:38:32
 */
@RestController
@RequestMapping("shopTemplate")
public class ShoppingShopTemplateController {

    @Autowired
    private ShoppingShopTemplateService shoppingShopTemplateService;

    @Value("${url.iconFileName}")
    private String iconFileName;

    @ApiOperation("店铺模板列表")
    @GetMapping("/queryShopTemplateList")
    @RequiresPermissions("shopTemplate:queryShopTemplateList")
    public CommonResult queryShopTemplateList(PageDTO pageDTO){
        PageUtils page = shoppingShopTemplateService.queryShopTemplateList(pageDTO);
        return Ref.ok(page);
    }


    @ApiOperation("店铺模板详情")
    @GetMapping("/getShopTemplateInfoById/{id}")
    @RequiresPermissions("shopTemplate:getShopTemplateInfoById")
    public CommonResult getShopTemplateInfoById(@PathVariable("id") Long id){
		ShoppingShopTemplate shoppingShopTemplate = shoppingShopTemplateService.getShopTemplateInfoById(id);
        return Ref.ok(shoppingShopTemplate);
    }

    @SysLog("新增店铺模板")
    @ApiOperation("新增店铺模板")
    @PostMapping("/addShopTemplate")
    @RequiresPermissions("shopTemplate:addShopTemplate")
    public CommonResult addShopTemplate(@RequestBody ShoppingShopTemplate shoppingShopTemplate){
		shoppingShopTemplateService.addShopTemplate(shoppingShopTemplate);
        return Ref.ok();
    }

    @ApiOperation("修改店铺模板")
    @PostMapping("/modifyShopTemplateById")
    @RequiresPermissions("shopTemplate:modifyShopTemplateById")
    public CommonResult modifyShopTemplateById(@RequestBody ShoppingShopTemplate shoppingShopTemplate){
		shoppingShopTemplateService.modifyShopTemplateById(shoppingShopTemplate);
        return Ref.ok();
    }

    @SysLog("删除店铺模板")
    @ApiOperation("删除店铺模板")
    @PostMapping("/removeShopTemplateById")
    @RequiresPermissions("shopTemplate:removeShopTemplateById")
    public CommonResult removeShopTemplateById(@RequestBody Long[] ids){
		shoppingShopTemplateService.removeShopTemplateById(Arrays.asList(ids));
        return Ref.ok();
    }

    @ApiOperation("获取店铺模板下的分类列表")
    @GetMapping("/getShopClassListById")
    public CommonResult getShopClassListById(PageDTO pageDTO){
        PageUtils vo = shoppingShopTemplateService.getShopClassListById(pageDTO);
        return Ref.ok(vo);
    }

    @ApiOperation("获取店铺模板下的分类详情")
    @GetMapping("/getShopClassDetailsById/{id}")
    public CommonResult getShopClassDetailsById(@PathVariable("id") Long id){
        ShopClassVO vo = shoppingShopTemplateService.getShopClassDetailsById(id);
        return Ref.ok(vo);
    }

    @SysLog("修改店铺模板下的分类详情")
    @ApiOperation("修改店铺模板下的分类详情")
    @PostMapping("/modifyShopClassDetailsById")
    public CommonResult modifyShopClassDetailsById(@RequestBody ShopClassVO shopClassVO){
        shoppingShopTemplateService.modifyShopClassDetailsById(shopClassVO);
        return Ref.ok();
    }

    @SysLog("新增店铺模板下的分类")
    @ApiOperation("新增店铺模板下的分类")
    @PostMapping("/addShopClass")
    public CommonResult addShopClass(@RequestBody ShopClassVO shopClassVO){
        shoppingShopTemplateService.addShopClass(shopClassVO);
        return Ref.ok();
    }

    @SysLog("删除店铺模板下的分类")
    @ApiOperation("删除店铺模板下的分类")
    @PostMapping("/removeShopClassById")
    public CommonResult removeShopClassById(@RequestBody Long[] ids){
        shoppingShopTemplateService.removeShopClassById(Arrays.asList(ids));
        return Ref.ok();
    }

    @ApiOperation("商品库商品")
    @GetMapping("queryCommodityBank")
    public CommonResult queryCommodityBank(CommodityBankDTO commodityBankDTO) {
        PageUtils page = shoppingShopTemplateService.queryCommodityBank(commodityBankDTO);
        return Ref.ok(page);
    }

    @SysLog("添加商品到模板分类")
    @ApiOperation("添加商品到模板分类")
    @PostMapping("addShopClassGoods/{id}")
    public CommonResult addShopClassGoods(@PathVariable String id, @RequestBody List<String> goodsId) {
        shoppingShopTemplateService.addShopClassGoods(id, goodsId);
        return Ref.ok();
    }

    @ApiOperation("店铺商品列表")
    @GetMapping("queryShopClassGoodsList")
    public CommonResult queryShopClassGoodsList(CommodityBankDTO commodityBankDTO) {
        PageUtils page = shoppingShopTemplateService.queryShopClassGoodsList(commodityBankDTO);
        return Ref.ok(page);
    }

    @SysLog("删除分类下商品")
    @ApiOperation("删除分类下商品")
    @PostMapping("removeShopClassGoodsByShopIdAndGoodsId/{id}")
    public CommonResult removeShopClassGoodsByShopIdAndGoodsId(@PathVariable String id, @RequestBody String[] goodsIds) {
        shoppingShopTemplateService.removeShopClassGoodsByShopIdAndGoodsId(id, Arrays.asList(goodsIds));
        return Ref.ok();
    }

    @SysLog("店铺模板启用")
    @ApiOperation("店铺模板启用")
    @PostMapping("modifyShopTemplateStateById")
    public CommonResult modifyShopTemplateStateById(@RequestBody CommodityBankDTO commodityBankDTO) {
        shoppingShopTemplateService.modifyShopTemplateStateById(commodityBankDTO.getId(), commodityBankDTO.getType());
        return Ref.ok();
    }

    @ApiOperation("上传icon图标到服务器")
    @RequestMapping("/uploadIcon")
    public CommonResult uploadIcon(HttpServletRequest request){
        if(!(request instanceof MultipartHttpServletRequest)){
            return Ref.error("no file upload");
        }
        MultipartHttpServletRequest req = (MultipartHttpServletRequest)request;
        Iterator<String> fileNames = req.getFileNames();
        OSSClientUtil ossClientUtil;
        List<UploadItem.UploadV0> items = new ArrayList<>();
        while (fileNames.hasNext()){
            String name = fileNames.next();
            List<MultipartFile> files = req.getFiles("file");
            if(files==null || files.size()==0){
                return Ref.error("param:"+name+",no file upload");
            }
            UploadItem.UploadV0 item = new UploadItem.UploadV0();
            for(MultipartFile file : files){
                if(file.getSize() <= 0){
                    continue;
                }
                ossClientUtil = new OSSClientUtil();
                try {
                    String icon = ossClientUtil.uploadImg2Oss(file,iconFileName);
                    if ("fail".equals(icon))
                        return Ref.error("上传失败");
                    String url = iconFileName + "/"+icon;
                    item.setName(name);
                    item.getUrlList().add(url);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            items.add(item);
        }
        return Ref.ok(items);
    }

}
